/*---------------------------------------------------------------------------------------------------------*/
/*                                                                                                         */
/* Copyright(c) 2019 Nuvoton Technology Corp. All rights reserved.                                         */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/

/***********************************************************************************************************/
/* Website: http://www.nuvoton.com                                                                         */
/*  E-Mail : MicroC-8bit@nuvoton.com                                                                       */
/*  Date   : Jan/21/2019                                                                                   */
/***********************************************************************************************************/

//***********************************************************************************************************
//  File Function: MS51 GPIO toggle demo code
//***********************************************************************************************************
#include "MS51_16K.H"
u8 temp _at_ 0x08;
u8 idata itemp _at_ 0x90;
u8 xdata xtemp _at_ 0x80;
u8  TimeCnt_1ms;
u8  TimeCnt_20ms;
u8   Flag_1s;
u8   Flag_20ms;
u8   Flag_1ms;
u8   Flag_OverZero_Pass;
u8   Flag_OverZero_Fail;
u8   Cnt_OverZero_Pass;
u8   Cnt_OverZero_Fail;
u16 pluseus=0;
static u8 i=0;
u8 j=0;
//---Capture_ISR------------------------------------------------------------------------------------------//
void Capture_ISR (void) interrupt 12
{
    _push_(SFRS);
  
    pluseus = C0H;                            /* For capture mode CxL/CxH with data capture from I/O pin*/
    pluseus = pluseus<<8;
	pluseus |= C0L;
	TH2=0;
	TL2=0;				  
    printf("%u\r\n",pluseus);
   if((pluseus>=19800)&&(pluseus<=20100))
	{
	  Cnt_OverZero_Fail=0;
	  Cnt_OverZero_Pass++;	    
	}
	else
	{
	   Cnt_OverZero_Pass=0;
	   Cnt_OverZero_Fail++;
	}	
    clr_CAPCON0_CAPF0;                  /*clear capture2 interrupt flag*/	                            
//    clr_T2CON_TF2;
    _pop_(SFRS);
}	  
//---Timer0_ISR------------------------------------------------------------------------------------------//
void Timer0_ISR (void) interrupt 1  //interrupt address is 0x000B
{
    TH0=TH0_INIT;
    TL0=TL0_INIT;    
    TF0=0;
	TimeCnt_1ms++;
	Flag_1ms=1;
    if(TimeCnt_1ms>=20)
	{
		TimeCnt_1ms=0;
		Flag_20ms=1;
		TimeCnt_20ms++;
	}
	if(TimeCnt_20ms>=50)
	{
		TimeCnt_20ms=0;
		Flag_1s=1;
	}
}
//---GPIO_Init------------------------------------------------------------------------------------------//
void GPIO_Init(void)
{
	P15_INPUT_MODE;
	P00_PUSHPULL_MODE;
	P01_PUSHPULL_MODE;
	P03_PUSHPULL_MODE;
	P04_PUSHPULL_MODE;
	P05_PUSHPULL_MODE;
	P07_PUSHPULL_MODE;
	P10_PUSHPULL_MODE;
	P11_PUSHPULL_MODE;
	P12_PUSHPULL_MODE;
	P13_PUSHPULL_MODE;
	P14_PUSHPULL_MODE;
	P17_PUSHPULL_MODE;
	P30_PUSHPULL_MODE;

	P16_QUASI_MODE;
    P02_INPUT_MODE;
}
//---WDT_Reset_Init-------------------------------------------------------------------------------------------//
void WDT_Reset_Init(void)
{
	TA=0xAA;
	TA=0x55;
	WDCON=0x03;                 //Setting WDT prescale,WDT will reset if  not clear WDCLK within 100ms
	set_WDCON_WDCLR;
}
//---Main-------------------------------------------------------------------------------------------//
void main (void)
{	
	WDT_Reset_Init(); 
  	GPIO_Init();
	P15 = 1;
	TIMER2_CAP0_Capture_Mode;
	IC7_P15_CAP0_FALLINGEDGE_CAPTURE;
//	IC7_P15_CAP0_RISINGEDGE_CAPTRUE;
//	IC7_P15_CAP0_BOTHEDGE_CAPTURE;
	MODIFY_HIRC(HIRC_16); 
    UART_Open(16000000,UART1_Timer3,4800);
    ENABLE_UART1_PRINTF;
	TIMER2_DIV_16;
    set_EIE_ECAP;                        //Enable Capture interrupt
    set_T2CON_TR2;                       //Triger Timer2
  	ENABLE_TIMER0_MODE1;
    TIMER0_FSYS;
    TH0 = TH0_INIT;
    TL0 = TL0_INIT;    
    ENABLE_TIMER0_INTERRUPT;                       //enable Timer0 interrupt
    ENABLE_GLOBAL_INTERRUPT;                       //enable interrupts 
    set_TCON_TR0;                                  //Timer0 run 
  while(1)
  {	
/*  	 if(Flag_1ms)	  
	   { 
	   	 Flag_1ms=0;
			 SegScan_Pass1();  
		} 	 */
 	if(Cnt_OverZero_Pass>=10)
	{	
		Cnt_OverZero_Pass=0;
		SegScan_Pass();
//		Flag_OverZero_Pass=1;
	}
	if(Cnt_OverZero_Fail>=2)
	{	
		Cnt_OverZero_Fail=0;
		SegScan_Fail();
//		Flag_OverZero_Pass=1;			
	}					 				   	   	  		 	
  }
}



